<template>
  <a-modal
    :title="modalTitle"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
    @cancel="handleCancel">
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item label="菜单ID" :labelCol="labelCol" :wrapperCol="wrapperCol" v-show="false">
          <a-input v-if="modalOpera==1" v-decorator="['id', {rules: [{required: true}]}]" />
        </a-form-item>

        <a-form-item label="菜单类型" :labelCol="labelCol" :wrapperCol="wrapperCol" v-show="false">
          <a-input v-decorator="['type',{rules: [{required: true, message: '请输入菜单类型！'}]}]" />
        </a-form-item>

        <a-form-item label="父级菜单" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-model="parentName" readonly />
        </a-form-item>

        <a-form-item label="父级菜单ID" :labelCol="labelCol" v-show="false">
          <a-input v-decorator="['parentId']" />
        </a-form-item>

        <a-form-item :label="labelMenu(type)+'名称'" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['name', {rules: [{required: true, min: 3, message: '请输入至少三个字符的菜单名称！'}]}]" />
        </a-form-item>

        <a-form-item label="定向菜单" :labelCol="labelCol" :wrapperCol="wrapperCol" v-if="type===0">
          <a-input placeholder="访问目录地址时重定向某个菜单" v-decorator="['redirect', {rules: [{required: false, min: 3, message: '请输指向某个菜单的地址！'}]}]" />
        </a-form-item>

        <a-form-item :label="labelMenu(type)+'图标'" :labelCol="labelCol" :wrapperCol="wrapperCol" v-if="type!==2">
          <a-input placeholder="如:apple" v-decorator="['icon', {rules: [{required: false, min: 3, message: '请输ICON！'}]}]" />
        </a-form-item>

        <a-form-item label="菜单组件" :labelCol="labelCol" :wrapperCol="wrapperCol" v-if="type===1">
          <a-input placeholder="访问菜单是访问的组件入口" v-decorator="['component', {rules: [{required: true, min: 3, message: '请输菜单组件！'}]}]" />
        </a-form-item>

        <a-form-item label="路由地址" :labelCol="labelCol" :wrapperCol="wrapperCol" v-if="type!==2">
          <a-input placeholder="如:user" v-decorator="['path', {rules: [{required: true, min: 3, message: '请输入至少三个字符的菜单名称！'}]}]" />
        </a-form-item>

        <a-form-item label="权限标识" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['perms', {rules: [{required: true, min: 4, message: '请输入至少四个字符的规则描述！'}]}]" />
        </a-form-item>

        <a-form-item label="API接口" v-if="apivisible" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input placeholder="多个接口地址用逗号隔开" v-decorator="['url', {rules: [{required: true, min: 4, message: '请输入至少四个字符的规则描述！'}]}]" />
        </a-form-item>

        <a-form-item label="菜单顺序" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input-number style="width: 100%;" :min="1" :max="1000" v-decorator="['orderNum',{rules: [{required: true, message: '请输入顺序'}],initialValue:1}]" />
        </a-form-item>

        <a-form-item label="路由缓存" v-if="type===1" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-switch
            checked-children="缓存"
            un-checked-children="实时"
            v-decorator="['keepAlive',{ rules: [{required: true}],initialValue: true,valuePropName: 'checked'}]"
          />
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import {
    save,
    getById,
    update
  } from '@/api/system/menu'

  import pick from 'lodash.pick'
  export default {
    data () {
      return {
        labelCol: {
          xs: {
            span: 24
          },
          sm: {
            span: 7
          }
        },
        wrapperCol: {
          xs: {
            span: 24
          },
          sm: {
            span: 13
          }
        },
        type: 0,
        modalTitle: '',
        modalOpera: 0, // 0：新增 1：编辑
        visible: false,
        apivisible: false,
        confirmLoading: false,
        parentName: '根目录',
        form: this.$form.createForm(this)
      }
    },
    created () {
    },
    methods: {
      add (record) {
        const title = record ? this.labelMenu(record.attributes.type + 1) : '目录'
        this.modal('新建' + title, 0)
        if (record) {
          this.parentName = record.name
          this.apivisible = true
          this.setVal(record.id, record.attributes.type + 1)
        } else {
          this.parentName = '根目录'
          this.apivisible = false
          this.setVal('0', 0)
        }
      },
      setVal (parentId, type) {
        const that = this
        setTimeout(() => {
          that.form.setFieldsValue({
            parentId: parentId
          })
          that.form.setFieldsValue({
            type: type
          })
        }, 100)
        this.type = type
      },
      labelMenu (type) {
        if (type === 0) {
          return '目录'
        } else if (type === 1) {
          return '菜单'
        } else if (type === 2) {
          return '按钮'
        }
      },
      edit (record) {
        const title = record ? this.labelMenu(record.attributes.type) : '目录'
        this.modal('编辑' + title, 1)
        getById({
          id: record.id
        }).then(response => {
          this.apivisible = response.res.type !== 0
          this.parentName = response.res.parentName
          this.type = response.res.type
          const that = this
          setTimeout(() => {
            that.form.setFieldsValue(
              pick(response.res,
                'id',
                'name',
                'perms',
                'url',
                'path',
                'icon',
                'orderNum',
                'parentId',
                'application',
                'component',
                'redirect',
                'keepAlive'
              )
            )
            that.form.setFieldsValue({
              type: response.res.type
            })
          }, 200)
        })
      },
      modal (title, opera) {
        this.modalTitle = title
        this.modalOpera = opera
        this.visible = true
        this.form.resetFields()
      },
      handleSubmit () {
        const {
          form: {
            validateFields
          }
        } = this
        this.confirmLoading = true
        validateFields((errors, values) => {
          if (!errors) {
            if (this.modalOpera === 0) {
              save(values).then(response => {
                if (response.code === 0) {
                  this.$message.info(response.msg)
                  this.$emit('ok')
                  this.handleCancel()
                } else {
                  this.$message.error(response.msg)
                }
              })
            } else if (this.modalOpera === 1) {
              update(values).then(response => {
                if (response.code === 0) {
                  this.$message.info(response.msg)
                  this.$emit('ok')
                  this.handleCancel()
                } else {
                  this.$message.error(response.msg)
                }
              })
            }
          }
          console.log(errors)
          this.confirmLoading = false
        })
      },
      handleCancel () {
        this.visible = false
        this.confirmLoading = false
      }
    }
  }
</script>
